var keyId;
var MiningId;
var fdPlanYear;
var fdPlanMonth;

efform_onload = function ()
{
	// 判断是否从生产实绩页面进入,如从该页面进入隐藏查询区域
	var args = new Object();
	args = GetUrlParms();	
	if(args["keyId"]!=undefined){
		keyId = args["keyId"];
		$('#ef_region_inqu').hide(); 
	}
	if(args["MiningId"]!=undefined){
		MiningId = args["MiningId"];
	}
	if(args["fdPlanYear"]!=undefined){
		fdPlanYear = args["fdPlanYear"];
	}
	if(args["fdPlanMonth"]!=undefined){
		fdPlanMonth = args["fdPlanMonth"];
	}
	//alert(getNowDate());
	$('#result_add-0-fdSubmitor').val($('#fdCreator').val());
	$('#result_add-0-fdSubmitorId').val($('#fdCreatorId').val());
	$('#result_add-0-fdCreateUser').val($('#fdCreator').val());
	$('#result_add-0-fdCreateUserId').val($('#fdCreatorId').val());
	$('#result_add-0-fdSubmitorDepartmentId').val($('#fdCreateDepartmentId').val());
	$('#result_add-0-fdSubmitorDepartment').val($('#fdCreateDepartment').val());

//	$('#result_add-0-fdProblemDate').val(getNowDate());
};
/*根据主表查询明细*/
efgrid_onRowClicked = function( grid_id, row_index ){ 
	if(grid_id == "ef_grid_result"){ 
		var grid = efgrid.getGridObject(grid_id); 
		var selectedRow=grid.getRowData(row_index);
		for(var item in selectedRow){
			var colname=item;
			var colvalue=selectedRow[item];
//			if("fdProblemDate" == item || "fdDeadlineDate" == item){
//				$('#ef_region_mainadd').find('#result_add-0-'+colname+'Time').val(colvalue);
//			}
			//alert("result_add-0-"+colname+"  "+colvalue);
			//alert($('#result_add-0-'+colname).val());
			$('#ef_region_mainadd').find('#result_add-0-'+colname).val(colvalue);
		} 
		
		var fdProblemDateTime = selectedRow['fdProblemDate']+' '+selectedRow['fdProblemTime'];
		var fdDeadlineDateTime = selectedRow['fdDeadlineDate']+' '+selectedRow['fdDeadlineTime'];
		$('#ef_region_mainadd').find('#result_add-0-fdProblemDateTime').val(fdProblemDateTime);
		$('#ef_region_mainadd').find('#result_add-0-fdDeadlineDateTime').val(fdDeadlineDateTime);
	}
}


var ajax_callback = {
		onSuccess : function(eiInfo) {
			var grid = efgrid.getGridObject("ef_grid_result");
			grid.refresh(eiInfo);
			clearAfterSolve("ef_region_mainadd");	
			$("#result_add-0-fdProblemStatus").val('0');
			$('#result_add-0-fdSubmitor').val($('#fdCreator').val());
			$('#result_add-0-fdSubmitorId').val($('#fdCreatorId').val());
			$('#result_add-0-fdCreateUser').val($('#fdCreator').val());
			$('#result_add-0-fdCreateUserId').val($('#fdCreatorId').val());
			$('#result_add-0-fdSubmitorDepartmentId').val($('#fdCreateDepartmentId').val());
			$('#result_add-0-fdSubmitorDepartment').val($('#fdCreateDepartment').val());
		}, 
		onFail:function(eMsg)
		{
			alert("确认失败，原因："+eMsg);
		}

}	

button_insert_onclick = function (){    
	var isCheck = check();
  	if (isCheck == 0) {
        return;
    }

	var info = getDetail("ef_region_mainadd","result_add");
	info.setByNodeObject(document.forms[0]);
  	
	if (null != info) {
	  	info.set("keyId",keyId);
		info.set("MiningId",MiningId);
		info.set("fdPlanYear",fdPlanYear);
		info.set("fdPlanMonth",fdPlanMonth);
		EiCommunicator.send("ZMCC00", "insert", info, ajax_callback);
	}
}

button_update_onclick = function (){
	//只有草拟状态可以修改
  	var isCheckForUpdate= checkForUpdate();
  	if (isCheckForUpdate == 0) {
        return;
    }
  	
	var isCheck = check();
  	if (isCheck == 0) {
        return;
    }

  	
	var info = getDetail("ef_region_mainadd","result_add");
	info.setByNodeObject(document.forms[0]);
	if (null != info) {
	  	info.set("keyId",keyId);
		info.set("MiningId",MiningId);
		info.set("fdPlanYear",fdPlanYear);
		info.set("fdPlanMonth",fdPlanMonth);
		EiCommunicator.send("ZMCC00", "update", info, ajax_callback);
	}  
}


button_query_onclick = function (){
	efgrid.submitInqu( "ef_grid_result", "ZM","ZMCC00","query");
}

button_delete_onclick = function (){
	if (isSelected("ef_grid_result") == 0)return; 
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
//		efgrid.submitForm( "ef_grid_result", "ZM","ZMCC00","delete",true);
		var grid=isSelected("ef_grid_result");
		if (grid == 0)return; 
		var rows=grid.getCheckedRows();
		for(var i=0;i<rows.length;i++)
		{
			if(grid.getCellValueByColumnName(rows[i],"fdProblemStatus")!="0") // 草拟0
			{
				EFAlert("只能对草拟的记录进行删除！"); return;
			}
		}
		
		var info = getSCZXEiInfos("ZMCC00","ef_grid_result");
		info.setByNodeObject(document.forms[0]);
		if (null != info) { 
			EiCommunicator.send("ZMCC00", "delete", info, validate_callback);
		}
	});
}
button_report_onclick = function (){
//	if (isSelected("ef_grid_result") == 0)
//		return; 
//	efgrid.submitForm( "ef_grid_result", "ZM","ZMCC00","report",true);
	var grid=isSelected("ef_grid_result");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdProblemStatus")!="0") // 草拟0
		{
			EFAlert("只能对草拟的记录进行上报！"); return;
		}
	}
	
	var info = getSCZXEiInfos("ZMCC00","ef_grid_result");
	info.setByNodeObject(document.forms[0]);
	if (null != info) { 
	  	info.set("keyId",keyId);
		info.set("MiningId",MiningId);
		info.set("fdPlanYear",fdPlanYear);
		info.set("fdPlanMonth",fdPlanMonth);
		EiCommunicator.send("ZMCC00", "report", info, validate_callback);
	}
}
var validate_callback={
		onSuccess:function(eiInfo){
			if(eiInfo.getStatus()==-1){
				EFAlert("操作失败，原因："+eiInfo.getMsg());
			}else{
				 efwindow.hide();
				 var grid = efgrid.getGridObject("ef_grid_result");
				 grid.refresh(eiInfo);
			}
		},
		onFail:function(eMsg)
		{
			EFAlert("操作失败，原因："+eMsg); 
		}
}
//填写日期时间
function changeDateAndTime(blockName,fieldPre)
{
	var dateTime=$('#'+blockName+'-0-'+fieldPre+'DateTime').val();
	var date=dateTime.substring(0,10);
	var time=dateTime.substring(11);
	$('#'+blockName+'-0-'+fieldPre+'Date').val(date);
	$('#'+blockName+'-0-'+fieldPre+'Time').val(time);
}

/*
 * 生产问题--输入项数据校验
 */
function check() {
	var fdProblemStatus = $("#result_add-0-fdProblemStatus").val(); // 生产问题状态
	var fdProblemDateTime = $("#result_add-0-fdProblemDateTime").val(); // 发生时间
	var fdDeadlineDateTime = $("#result_add-0-fdDeadlineDateTime").val(); // 解决时间

	var fdProblemPlace = $("#result_add-0-fdProblemPlace").val().trim(); // 发生地点
	var fdProblemDescription = $("#result_add-0-fdProblemDescription").val().trim(); // 问题描述
	var fdBusinessMan = $("#result_add-0-fdBusinessMan").val().trim(); // 责任人
	var fdRemarks = $("#result_add-0-fdRemarks").val().trim(); // 备注

		
	if (fdProblemPlace == "") {
		SCZXAlert("发生地点不能为空！");
		return false;
	} else if (fdProblemPlace.getBytes() > 250 || fdProblemPlace.getBytes() < 1) {
		SCZXAlert("[发生地点]请输入1~250个字符！");
		return false;
	}

	if (fdBusinessMan == "") {
		SCZXAlert("责任人不能为空！");
		return false;
	} else if (fdBusinessMan.getBytes() > 10 || fdBusinessMan.getBytes()< 1) {
		SCZXAlert("[责任人]请输入1~10个字符！");
		return false;
	}
	
	if (fdProblemDescription == "") {
		SCZXAlert("问题描述不能为空！");
		return false;
	} else if (fdProblemDescription.getBytes() > 400
			|| fdProblemDescription.getBytes() < 1) {
		SCZXAlert("[问题描述]请输入1~400个字符！");
		return false;
	}

	if (fdRemarks.getBytes() > 250) {
		SCZXAlert("备注不能超过250个字符！");
		return false;
	}

	if (fdProblemDateTime == "") {
		SCZXAlert("发生时间不能为空！");
		return false;
	}
	if (fdDeadlineDateTime == "" || fdDeadlineDateTime == 0) {
		SCZXAlert("解决时间不能为空！");
		return false;
	} else {
		if (getDateValue(fdProblemDateTime) > getDateValue(fdDeadlineDateTime)) {
			SCZXAlert("发生时间不能晚于解决时间，请重新输入！");
			return false;
		}
	}

	return true;
}
function checkForUpdate() {
	var fdProblemStatus = $("#result_add-0-fdProblemStatus").val(); // 生产问题状态


	if(fdProblemStatus != 0){
		SCZXAlert("只有状态为草拟的记录可以修改！");
		return false;
	}
	return true;
}

String.prototype.getBytes = function() {      
    var cArr = this.match(/[^\x00-\xff]/ig);      
    return this.length + (cArr == null ? 0 : cArr.length);   
      
}  

String.prototype.trim=function(){
    return this.replace(/(^\s*)|(\s*$)/g, "");
 }
/*
根据传入的String类型的date获取该日期对应的毫秒值，传入的字符串格式为：yyyy-MM-dd HH:mm:ss
*/
function getDateValue(dateStr) {
   var tt = dateStr.split(" ");
   var ttt1 = tt[0].split("-");
   var ttt2 = tt[1].split(":");
   var date1 = new Date(ttt1[0], ttt1[1], ttt1[2], ttt2[0], ttt2[1], ttt2[2]);
   var m1 = date1.getTime();
   return m1;
}

function GetUrlParms(){
	var args=new Object();   
    var query=location.search.substring(1);// 获取查询串
    var pairs=query.split("&");// 在逗号处断开
    for(var   i=0;i<pairs.length;i++)   
    {   
        var pos=pairs[i].indexOf('=');// 查找name=value
            if(pos==-1)   continue;// 如果没有找到就跳过
            var argname=pairs[i].substring(0,pos);// 提取name
            var value=pairs[i].substring(pos+1);// 提取value
            args[argname]=unescape(value);// 存为属性
    }
    return args;
}
/**
 * 操作完成后清空该region内的各数据项的值
 */
var clearAfterSolve=function(region) { 
	var temp;
	//遍历INPUT
	$('#'+region).find("input").each(function (){
		temp = $(this).attr("id");
		$('#'+temp).val("");
//		$(this).removeData();
	});
	//遍历SELECT
	$('#'+region).find("select").each(function (){
		temp = $(this).attr("id");
		$('#'+temp).val("");
	});
	//遍历TEXTAREA
	$('#'+region).find("textarea").each(function (){
		temp = $(this).attr("id");
		$('#'+temp).val("");
	});
};